"""
2017.06.05
读取文件，使用字典dict保存数据，并输出
"""


def sanitize(time_string):
    """
    过滤(净化)
    :param time_string:
    :return:
    """
    if '-' in time_string:
        splitter = '-'
    elif ':' in time_string:
        splitter = ':'
    else:
        return time_string
    (mins, secs) = time_string.split(splitter)
    return mins + '.' + secs


# 读取教练的数据
def get_coach_data(filename):
    try:
        with open(filename) as f:
            data = f.readline().strip().split(',')
        return {'Name': data.pop(0),
                'Dob': data.pop(0),
                'Times': str(sorted(set([sanitize(t) for t in data]))[0:3])}
    except IOError as err:
        print('File error: ' + str(err))
        return None


# 将sarah数据文件转为一个列表
sarah = get_coach_data('sarah2.txt')
james = get_coach_data('james2.txt')
mikey = get_coach_data('mikey2.txt')
julie = get_coach_data('julie2.txt')
print(sarah['Name'] + "'s fastest times are: " + sarah['Times'])
print(james['Name'] + "'s fastest times are: " + james['Times'])
print(mikey['Name'] + "'s fastest times are: " + mikey['Times'])
print(julie['Name'] + "'s fastest times are: " + julie['Times'])

# Sarah Sweeney's fastest times are: ['2.18', '2.21', '2.22']
# James Lee's fastest times are: ['2.01', '2.16', '2.22']
# Mikey McManus's fastest times are: ['2.22', '2.31', '2.38']
# Julie Jones's fastest times are: ['2.11', '2.23', '2.59']